package com.test.service;

import com.test.dao.ITimingTaskSettingDao;
import com.test.model.TimingTaskSetting;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by ZZF on 2017/6/28.
 */
@Service
public class AsyncTaskService {

    private static Logger logger = Logger.getLogger(AsyncTaskService.class);

    @Autowired
    ITimingTaskSettingDao timingTaskSettingDao;
    @Autowired
    IHeatingSeasonService heatingSeasonService;

    @Autowired
    WriteDataService writeDataService;

    @Async
    public void executeAsyncTask() {


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String format = df.format(new Date());// new Date()为获取当前系统时间
        Integer integer = Integer.valueOf(format.substring(8, 10));
        String tasktime = format.substring(11);

        //设置凌晨这个时间点
        String zero = "17:19:00";

        boolean isZero = false;

        boolean isWrite = false;

        if (tasktime.equals(zero)) {
            isZero = true;
            isWrite = true;


        } else {
            //取的所有的任务时间
            List<TimingTaskSetting> taskAll = timingTaskSettingDao.getAll(TimingTaskSetting.class);


            if (!taskAll.isEmpty() && isZero == false) {
                for (TimingTaskSetting timingTaskSetting : taskAll) {
                    //时间方式
                    String taskType = timingTaskSetting.getTaskTimingType();
                    //任务时间
                    Time taskTime = timingTaskSetting.getTaskTime();
                    //任务时间转化为String
                    String time = taskTime.toString();
                    //任务日期
                    Integer taskDate = timingTaskSetting.getTaskDate();

                    switch (taskType) {
                        case "月":
                            if (integer.equals(taskDate)) {
                                if (tasktime.equals(time)) {
                                    isWrite = true;
                                    logger.info("写一次库——月");
                                }
                            }
                            break;
                        case "周":
                            if (taskDate.equals(getCurrentWeek())) {
                                if (tasktime.equals(time)) {
                                    isWrite = true;
                                    logger.info("写一次库——周");
                                }
                            }
                            break;
                        case "日":
                            if (tasktime.equals(time)) {
                                isWrite = true;
                                logger.info("写一次库——日");
                            }
                            break;

                    }

                }
            }

        }

        //验证是否是采暖季的第一次底数记录
        if (isZero) {
            heatingSeasonService.updateHeatingSeason();
            logger.info("写一次库——零点");
        }

        if (isWrite) {
            writeDataService.writeDate();
            logger.info("写库");
        }



    }


    public static Integer getCurrentWeek() {
        String[] weeks = {"0", "1", "2", "3", "4", "5", "6"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        String week = weeks[week_index];

        return Integer.valueOf(week);
    }

}
